# Tests for theeventcal module
from datetime import datetime as dt
from datetime import timedelta
from eventcal.eventcal import *
import eventcal.eventcal
import unittest
from xml.dom.minidom import parseString

set_language('en', True) # english language in local locale directory

class EventTest(unittest.TestCase):

    def setUp(self):
        self.event_messages = ('Test message', 'hello!', 'old old date')
        self.event_starts = (dt(2008, 1, 1), dt(2006, 12, 31), dt(2003, 5, 21, hour=3))
        self.event_lengths = (timedelta(hours=3), timedelta(days=2), timedelta(minutes=45))
        self.event_ids = (-1, -1, -1)
        self.event_day_names = ('Tuesday', 'Sunday', 'Wednesday')
        self.event_strtime_output = ('12:00AM to 03:00AM', '12:00AM to 12:00AM', '03:00AM to 03:45AM')
        self.event_repr_output = ('[1/1/2008] 12:00AM to 03:00AM - Test message',
                                  '[31/12/2006] 12:00AM to 12:00AM - hello!',
                                  '[21/5/2003] 03:00AM to 03:45AM - old old date')
        self.len = len(self.event_messages)
        self.events = []
        for i in xrange(self.len):
            self.events.append(Event(self.event_messages[i],
                                     self.event_starts[i],
                                     self.event_lengths[i],
                                     self.event_ids[i]))

    def testday(self):
        for i in xrange(self.len):
            self.assertEqual(self.events[i].day, self.event_starts[i].day)

    def testmonth(self):
        for i in xrange(self.len):
            self.assertEqual(self.events[i].month, self.event_starts[i].month)

    def testyear(self):
        for i in xrange(self.len):
            self.assertEqual(self.events[i].year, self.event_starts[i].year)

    def testhour(self):
        for i in xrange(self.len):
            self.assertEqual(self.events[i].hour, self.event_starts[i].hour)

    def testdayName(self):
        set_language('en', True)
        for i in xrange(self.len):
            self.assertEqual(self.events[i].dayName, self.event_day_names[i])

    def testday_name(self):
        set_language('en', True)
        for i in xrange(self.len):
            self.assertEqual(self.events[i].day_name, self.event_day_names[i])

    def test__cmp__(self):
        i = 0
        while i < self.len - 1:
            self.assertEqual(self.events[i], self.events[i])
            self.assertNotEqual(self.events[i], self.events[i+1])
            i += 2

    def teststrtime(self):
        for i in xrange(self.len):
            self.assertEqual(self.events[i].strtime(), self.event_strtime_output[i])

    def test__repr(self):
        for i in xrange(self.len):
            self.assertEqual(repr(self.events[i]), self.event_repr_output[i])

class CalendarTest(unittest.TestCase):
    def setUp(self):
        self.cal = Calendar()
        self.events = [Event('abc', datetime.datetime(2009, 1, 1, 11, 30),
                             datetime.timedelta(hours=2)),
                       Event('def', datetime.datetime(2009, 3, 12, 21, 0),
                             datetime.timedelta(hours=4))]
        for event in self.events:
            self.cal.add(event)

    def testadd(self):
        for i in xrange(len(self.events)):
            self.assertEqual(self.cal.events[i], self.events[i])

    def testeventlist(self):
        doc = parseString(self.cal.eventlist()).documentElement
        self.assertEqual(doc.tagName, 'ul')
        i = len(self.events) - 1
        for child in doc.childNodes:
            i -= 1
            self.assertEqual(child.tagName, 'li')
            self.assertEqual(child.childNodes[0].nodeValue,
                             self.events[i].start.strftime('%A %B %d, %Y'))
            self.assertEqual(child.childNodes[1].tagName, 'br')
            self.assertEqual(child.childNodes[2].nodeValue,
                             '%s: %s' % (self.events[i].length, self.events[i].message))
        
    def testcompact_dayview(self):
        pass

    def dayview(self):
        pass

    def weekview(self):
        pass

    def monthview(self):
        pass

if __name__ == '__main__':
    unittest.main()
